<template>
  <div id="users" class="card row mb-22 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1" aria-current="page">系统管理</li>
        <li class="breadcrumb-item active" aria-current="page">员工</li>
      </ol>
    </nav>
    <template v-if="isSearchMode">
      <div class="card-body">
        <form>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="用户名"
              v-model.trim="username"
              size="10"
            />
          </div>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="手机"
              v-model.trim="mobile"
              size="10"
            />
          </div>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="eterm用户名"
              v-model.trim="etermUsername"
              size="10"
            />
          </div>
          <div class="row mb-2">
            状态
            <select class="form-select" v-model.number="status">
              <option value="-1">全部</option>
              <option value="0">未启用</option>
              <option value="1">正常</option>
              <option value="4">停用</option>
            </select>
          </div>
          <div class="row mb-2">
            <select class="form-select" v-model.number="sc.pageSize">
              <option value="25">25</option>
              <option value="50">50</option>
              <option value="100">100</option>
              <option value="500">500</option>
            </select>
          </div>
          <div class="row mb-2">
            <button
              type="button"
              class="btn btn-primary mb-2"
              @click.stop="search()"
            >
              确定
            </button>
            <button
              type="button"
              class="btn btn-secondary"
              @click.stop="reset()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
    </template>
    <template v-else>
      <div class="card-body">
        <button
          type="button"
          @click.stop="newUser()"
          class="btn btn-success"
          id="btnNewUser"
        >
          新建
        </button>
        <button
          type="button"
          class="btn btn-primary float-end"
          @click.stop="showSearch()"
        >
          查找
        </button>
      </div>
      <table class="table table-striped table-hover table-sm bg-transparent">
        <thead>
          <tr>
            <th class="text-center">用户名</th>
            <th class="text-center">全名</th>
            <th class="d-none d-md-table-cell">手机</th>
            <th class="d-none d-md-table-cell">邮件</th>
            <th class="d-none d-md-table-cell">部门</th>
            <th class="text-center d-none d-md-table-cell">企业微信</th>
            <th class="d-none d-md-table-cell">eterm用户名</th>
            <th class="d-none d-md-table-cell">状态</th>
            <th>客服组</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="(user, index) in userList"
            :key="user.username"
            @click.stop="showDetail(user.id)"
          >
            <td class="text-center">{{ user.username }}</td>
            <td class="text-center">{{ user.empname }}</td>
            <td class="d-none d-md-table-cell">{{ user.mobileNo }}</td>
            <td class="d-none d-md-table-cell">{{ user.email }}</td>
            <td class="d-none d-md-table-cell">
              <template v-for="info in user.departments">
                {{ info.name }},
              </template>
            </td>
            <td class="text-center d-none d-md-table-cell">
              <!-- {{user.wwUserId}} -->
              <span v-if="user.wwUserId.length > 0">
                <img :src="user.avatar" width="25" height="25" />
                <!-- <a href="javascript:void(0)" v-on:click="delWwUser(index);" class="text-danger">删除</a> -->
                <!-- <a href="javascript:void(0)" v-on:click="syncWwUser(index);">同步</a> -->
              </span>
            </td>
            <td class="d-none d-md-table-cell">
              <template v-if="user.etermUsername !== null">
                <template v-if="user.etermUsername.length > 10">
                  <span :title="user.etermUsername"
                    >{{ user.etermUsername.substring(0, 10) }}...</span
                  >
                </template>
                <template v-else>
                  {{ user.etermUsername }}
                </template>
              </template>
            </td>
            <td class="d-none d-md-table-cell">
              <span v-if="user.status === 1">正常</span>
              <span v-if="user.status === 4">停用</span>
            </td>
            <td>
              {{ user.groupName }}
            </td>
            <td>
              <i class="mdi mdi-arrow-right fa-2"></i>
            </td>
          </tr>
        </tbody>
      </table>
      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </template>
  </div>
</template>

<script>
import $ from "jquery";
import { mapActions } from "vuex";
import { searchUsers } from "@/api/admin.js";
import MyPagination from "@/components/my-pagination_h5.vue";

export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      isSearchMode: false,
      modalTitle: "",
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      username: "",
      mobile: "",
      etermUsername: "",
      status: -1,

      userList: [],
      user: {
        id: 0,
        username: "",
        empname: "",
        mobileNo: "",
        email: "",
        etermUsername: "",
        position: "",
        status: 0,
      },
      deptIds: [],
      syncWxWork: 1,
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    this.search();
  },
  methods: {
    ...mapActions(["showLoading", "hideLoading"]),
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showSearch: function () {
      this.isSearchMode = true;
    },
    search: function () {
      this.isSearchMode = false;
      this.showLoading();

      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.username": this.username,
        "sc.mobile": this.mobile,
        "sc.etermUsername": this.etermUsername,
        "sc.status": this.status,
      };

      searchUsers(
        params,
        (v) => {
          this.userList = v.dataList;
          this.sc = v.page;
        },
        () => this.hideLoading()
      );
    },
    showDetail: function (id) {
      this.$router.push("/h5/admin/user/" + id);
    },
    newUser: function () {
      this.$router.push("/h5/admin/user");
    },
    reset: function () {
      this.username = "";
      this.mobile = "";
      this.etermUsername = "";
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
